home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / include / glibmm-2.4 / glibmm / propertyproxy.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-04-20  |  4.8 KB  |  180 lines

  1. // -*- c++ -*-
  2. #ifndef _GLIBMM_PROPERTYPROXY_H
  3. #define _GLIBMM_PROPERTYPROXY_H
  4. /* $Id: propertyproxy.h,v 1.4 2005/06/08 16:06:13 murrayc Exp $ */
  5.  
  6. /* propertyproxy.h
  7.  *
  8.  * Copyright 2002 The gtkmm Development Team
  9.  *
  10.  * This library is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU Library General Public
  12.  * License as published by the Free Software Foundation; either
  13.  * version 2 of the License, or (at your option) any later version.
  14.  *
  15.  * This library is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.  * Library General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU Library General Public
  21.  * License along with this library; if not, write to the Free
  22.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24.  
  25. #include <glibmm/propertyproxy_base.h>
  26.  
  27.  
  28. namespace Glib
  29. {
  30.  
  31. /** A PropertyProxy can be used to get and set the value of an object's property.
  32.  * There are usually also get and set methods on the class itself, which you might find more convenient.
  33.  * With the PropertyProxy, you may use either get_value() and set_value(), or operator=() and
  34.  * operator PropertyType(), like so:
  35.  * @code
  36.  * int height = cellrenderer.property_height();
  37.  * cellrenderer.property_editable() = true;
  38.  * @endcode
  39.  *
  40.  * You may also receive notification when a property's value changes, by connecting to signal_changed().
  41.  */
  42. template <class T>
  43. class PropertyProxy : public PropertyProxy_Base
  44. {
  45. public:
  46.   typedef T PropertyType;
  47.  
  48.   PropertyProxy(ObjectBase* obj, const char* name)
  49.     : PropertyProxy_Base(obj, name) {}
  50.  
  51.   /** Set the value of this property.
  52.    * @param data The new value for the property.
  53.    */
  54.   void set_value(const PropertyType& data);
  55.  
  56.   /** Get the value of this property.
  57.    * @result The current value of the property.
  58.    */
  59.   PropertyType get_value() const;
  60.  
  61.   /** Set the value of this property back to its default value
  62.    */
  63.   void reset_value()
  64.     { reset_property_(); }
  65.  
  66.   PropertyProxy<T>& operator=(const PropertyType& data)
  67.     { this->set_value(data); return *this; }
  68.  
  69.   operator PropertyType() const
  70.     { return this->get_value(); }
  71. };
  72.  
  73.  
  74. /** See PropertyProxy().
  75.  * This property can be written, but not read, so there is no get_value() method.
  76.  */
  77. template <class T>
  78. class PropertyProxy_WriteOnly : public PropertyProxy_Base
  79. {
  80. public:
  81.   typedef T PropertyType;
  82.  
  83.   PropertyProxy_WriteOnly(ObjectBase* obj, const char* name)
  84.     : PropertyProxy_Base(obj, name) {}
  85.  
  86.   /** Set the value of this property.
  87.    * @param data The new value for the property.
  88.    */
  89.   void set_value(const PropertyType& data);
  90.  
  91.   /** Set the value of this property back to its default value
  92.    */
  93.   void reset_value()
  94.     { reset_property_(); }
  95.  
  96.   PropertyProxy_WriteOnly<T>& operator=(const PropertyType& data)
  97.     { this->set_value(data); return *this; }
  98. };
  99.  
  100. /** See PropertyProxy().
  101.  * This property can be read, but not written, so there is no set_value() method.
  102.  */
  103. template <class T>
  104. class PropertyProxy_ReadOnly : public PropertyProxy_Base
  105. {
  106. public:
  107.   typedef T PropertyType;
  108.  
  109.   //obj is const, because this should be returned by const accessors.
  110.   PropertyProxy_ReadOnly(const ObjectBase* obj, const char* name)
  111.     : PropertyProxy_Base(const_cast<ObjectBase*>(obj), name) {}
  112.  
  113.   /** Get the value of this property.
  114.    * @result The current value of the property.
  115.    */
  116.   PropertyType get_value() const;
  117.  
  118.   operator PropertyType() const
  119.     { return this->get_value(); }
  120. };
  121.  
  122.  
  123. /**** Template Implementation **********************************************/
  124.  
  125. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  126.  
  127. template <class T>
  128. void PropertyProxy<T>::set_value(const T& data)
  129. {
  130.   Glib::Value<T> value;
  131.   value.init(Glib::Value<T>::value_type());
  132.  
  133.   value.set(data);
  134.   set_property_(value);
  135. }
  136.  
  137. template <class T>
  138. T PropertyProxy<T>::get_value() const
  139. {
  140.   Glib::Value<T> value;
  141.   value.init(Glib::Value<T>::value_type());
  142.  
  143.   get_property_(value);
  144.   return value.get();
  145. }
  146.  
  147. //We previously just static_cast<> PropertyProxy_WriteOnly<> to PropertyProxy<> to call its set_value(), 
  148. //to avoid code duplication.
  149. //But the AIX compiler does not like that hack.
  150. template <class T>
  151. void PropertyProxy_WriteOnly<T>::set_value(const T& data)
  152. {
  153.   Glib::Value<T> value;
  154.   value.init(Glib::Value<T>::value_type());
  155.  
  156.   value.set(data);
  157.   set_property_(value);
  158. }
  159.  
  160. //We previously just static_cast<> PropertyProxy_WriteOnly<> to PropertyProxy<> to call its set_value(), 
  161. //to avoid code duplication.
  162. //But the AIX compiler does not like that hack.
  163. template <class T>
  164. T PropertyProxy_ReadOnly<T>::get_value() const
  165. {
  166.   Glib::Value<T> value;
  167.   value.init(Glib::Value<T>::value_type());
  168.  
  169.   get_property_(value);
  170.   return value.get();
  171. }
  172.  
  173. #endif /* DOXYGEN_SHOULD_SKIP_THIS */
  174.  
  175. } // namespace Glib
  176.  
  177.  
  178. #endif /* _GLIBMM_PROPERTYPROXY_H */
  179.  
  180.